package com.laundry.service.impl;

import com.laundry.dao.AnnouncementDao;
import com.laundry.entity.Announcement;
import com.laundry.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    private AnnouncementDao announcementDao;
    
    @Override
    public List<Announcement> findAll() {
        return announcementDao.findAll();
    }
    
    @Override
    public Announcement findById(Long id) {
        return announcementDao.findById(id).orElse(null);
    }
    
    @Override
    public Announcement save(Announcement announcement) {
        announcement.setCreateTime(new Date());
        announcement.setUpdateTime(new Date());
        return announcementDao.save(announcement);
    }
    
    @Override
    public Announcement update(Announcement announcement) {
        Announcement existAnnouncement = announcementDao.findById(announcement.getId()).orElse(null);
        if (existAnnouncement == null) {
            return null;
        }
        
        announcement.setCreateTime(existAnnouncement.getCreateTime());
        announcement.setUpdateTime(new Date());
        return announcementDao.save(announcement);
    }
    
    @Override
    public void deleteById(Long id) {
        announcementDao.deleteById(id);
    }
} 